<template>
  <div>
    <div class="area-header">
      <span class="area-header-title">{{ $t('上传财务数据') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('年份')" prop="year">
        <DatePicker v-model="queryParams.year" type="year" value-format="yyyy" :placeholder="$t('年份')" />
      </el-form-item>
      <el-form-item :label="$t('报告周期')" prop="type">
        <el-select v-model="queryParams.type" value="" :placeholder="$t('报告周期')" clearable>
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('数据来源')" prop="dataSource">
        <el-select v-model="queryParams.dataSource" value="" :placeholder="$t('数据来源')" clearable>
          <el-option
            v-for="dict in dataSourceOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['company:pcw:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="pcwDataMainList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('年份')" align="center" prop="year" min-width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.uploadType =='market'" class="badge">上市</div>
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.year }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('数据来源')" align="center" prop="dataSource" min-width="100" :formatter="dataSourceFormat" />
      <el-table-column :label="$t('报告周期')" align="center" prop="type" :formatter="typeFormat" min-width="100" />
      <el-table-column :label="$t('币种')" align="center" prop="currency" min-width="100" :formatter="currencyFormat" />
      <el-table-column :label="$t('点评意见')" align="center" prop="reviewCount" min-width="100">
        <template slot-scope="scope">
          <a
            class="link_a"
            @click="reviewList(scope.row)"
          >{{ scope.row.reviewCount }}</a>
        </template>
      </el-table-column>
      <el-table-column :label="$t('附件')" align="center" prop="attName">
        <template slot-scope="scope">
          <el-link v-if="scope.row.listFileBase && scope.row.listFileBase.length === 0" @click="onlinePreview(scope.row.fileId)">
            <span>{{ scope.row.fileName }}</span>
          </el-link>
          <div v-for="(file) in scope.row.listFileBase" v-else :key="file.id">
            <div class="pre" @click="onlinePreview(file.id)">
              {{ file.name }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('科目校验')" align="center" prop="uploadType" min-width="100">
        <template slot-scope="scope">
          <el-tag type="success">{{ getValidationPass(scope.row.validationPass,0) }}</el-tag>
          <el-tag type="danger">{{ getValidationPass(scope.row.validationPass,1) }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('录入类型')" align="center" prop="uploadType" min-width="100">
        <template slot-scope="scope">
          <span>{{ convetTypeName(scope.row.uploadType) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('上传人')" align="center" prop="createBy" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.createBy.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('上传时间')" align="center" prop="createTime" min-width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" width="160">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:pcw:view']"
            size="small"
            type="text"
            icon="el-icon-copy-document"
            @click="updateHandle(scope.row)"
          >{{ $t('详情') }}</el-button>
          <el-divider direction="vertical" />
          <el-dropdown>
            <el-button size="small" type="text">{{ $t('更多') }}<i class="el-icon-arrow-down el-icon--right" /></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPermi="['company:pcw:edit']">
                <a @click="updateForm(scope.row)">{{ $t('修改') }}</a>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['company:pcw:edit']">
                <a @click="reviewForm(scope.row)">{{ $t('点评') }}</a>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['company:pcw:export']">
                <a @click="exportHandle(scope.row)">{{ $t('导出') }}</a>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['company:pcw:edit']">
                <a @click="downloadFile(scope.row)">{{ $t('下载') }}</a>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['company:pcw:remove']">
                <a @click="deleteHandle(scope.row)">{{ $t('删除') }}</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <div>
            <el-button
              v-hasPermi="['company:pcw:edit']"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="updateForm(scope.row)"
            >{{ $t('修改') }}</el-button>
            <el-button
              v-hasPermi="['company:pcw:edit']"
              size="small"
              type="text"
              icon="el-icon-chat-dot-square"
              @click="reviewForm(scope.row)"
            >{{ $t('点评') }}</el-button>
            <el-button
              v-hasPermi="['company:pcw:view']"
              size="small"
              type="text"
              icon="el-icon-copy-document"
              @click="updateHandle(scope.row)"
            >{{ $t('详情') }}</el-button>
          </div>
          <div>
            <el-button
              v-hasPermi="['company:pcw:export']"
              size="small"
              type="text"
              icon="el-icon-document"
              @click="exportHandle(scope.row)"
            >{{ $t('导出') }}</el-button>
            <el-button
              v-hasPermi="['company:pcw:view']"
              size="small"
              type="text"
              icon="el-icon-download"
              @click="downloadFile(scope.row)"
            >{{ $t('下载') }}</el-button>
            <el-button
              v-hasPermi="['company:pcw:remove']"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >{{ $t('删除') }}</el-button>
          </div> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上传财务数据主表对话框 -->
    <el-dialog
      custom-class="el-dialog__wrapper"
      :title="$t('财务数据上传')"
      :visible.sync="formOpen"
      width="550px"
      :close-on-click-modal="false"
      :lock-scroll="false"
      append-to-body
    >
      <pcwDataMainUpload ref="pcwDataMainUpload" :company-id="companyId" @refresh="refresh" />
    </el-dialog>

    <!-- 财报点评对话框 -->
    <el-dialog :title="$t('财报点评')" :visible.sync="reviewFormOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="reviewDialogForm" append-to-body>
      <pcwReviewForm ref="pcwReviewForm" :company-id="companyId" />
    </el-dialog>

    <!-- 财报点评对话框 -->
    <el-dialog :title="$t('财报点评')" :visible.sync="reviewListOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <pcwReviewList ref="pcwReviewList" :company-id="companyId" />
    </el-dialog>

    <!-- 添加或修改上传财务数据主表对话框 -->
    <el-dialog :title="$t('修改')" :visible.sync="updateformOpen" width="30%" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <pcwDataMainUploadForm ref="pcwDataMainUploadForm" :company-id="companyId" />
    </el-dialog>

    <!--预览附件-->
    <OnlinePreview ref="OnlinePreview" />

    <el-dialog
      v-if="tabOpen"
      :visible.sync="tabOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      append-to-body
      :show-close="false"
      custom-class="pcwDialog"
    >
      <pcw-data-main-tab ref="pcwDataMainTab" :upload-data="tabResponseData" disabled :company-id="companyId" @cancel="tabOpen = false" @save="tabOpen = false" />
    </el-dialog>
    <el-dialog

      :visible.sync="handOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      append-to-body
      :show-close="false"
      top="50px"
      width="70%"
    >
      <div v-loading="handUploadLoading">
        <handUpload ref="handUpload" :company-id="companyId" :is-add="isAdd" :upload-data="uploadData" @save="tipSave" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPcwDataMain, delPcwDataMain, getUploadData, getPcwDataMainDataByHand, updataUploadByHand } from '@/api/company/pcw/pcwDataMain'
import pcwDataMainUpload from './pcwDataMainUpload.vue'
import pcwDataMainUploadForm from './pcwDataMainUploadForm.vue'
import pcwReviewForm from './pcwReviewForm.vue'
import pcwReviewList from './pcwReviewList.vue'
import config from '@/utils/config'
import { getToken } from '@/utils/auth'
import pcwDataMainTab from '@/views/company/pcw/pcwDataMainTab'
import handUpload from './handUpload.vue'
export default {
    components: {
        pcwDataMainUpload,
        pcwReviewForm,
        pcwReviewList,
        pcwDataMainTab,
        handUpload,
        pcwDataMainUploadForm
    },
  props: {
    companyId: {
      type: String,
      default: undefined
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 上传财务数据主表表格数据
          pcwDataMainList: [],
          // 选项卡
          activeName: 'second',
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
          // 财报点评formopen
          reviewFormOpen: false,
          // 财报点评formTitle
          reviewFormTitle: '',
          // 财报点评listOpen
          reviewListOpen: false,
          // 修改财报数据
          updateformOpen: false,
          // 财报点评listTitle
          reviewListTitle: '',
          // 查询参数
          queryParams: {
              companyId: this.companyId,
              year: undefined,
              type: undefined,
              dataSource: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 报告类型
          typeOptions: [],
          dataSourceOptions: [],
          currencyOptions: [],
          tabOpen: false,
          tabResponseData: {},
          handOpen: false,
          uploadData: {},
          isAdd: false,
          handUploadLoading: false,
          rowMainId: undefined,
          // 表单校验
          rules: {
          }
      }
  },
  created() {
      // 财务数据报告周期
      this.getDicts('financial_type').then(response => {
        this.typeOptions = response.data
      })
      this.getDicts('pcw_data_source').then(response => {
        this.dataSourceOptions = response.data
      })
      this.getDicts('currency').then(response => {
        this.currencyOptions = response.data
      })
      this.getList()
  },
  methods: {
    typeFormat(row) {
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    dataSourceFormat(row) {
      return this.selectDictLabel(this.dataSourceOptions, row.dataSource)
    },
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    refresh() {
      this.formOpen = false
      this.getList()
    },
    /** 查询上传财务数据主表列表 */
    getList() {
        this.loading = true
        listPcwDataMain(this.queryParams).then(response => {
            this.pcwDataMainList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: undefined,
            year: undefined,
            type: undefined,
            attachmentId: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
          this.$refs.pcwDataMainUpload.init()
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      if (row.uploadType === 'hand' || row.uploadType === 'market') {
           this.handOpen = true
           this.rowMainId = undefined
           this.handUploadLoading = true
              getPcwDataMainDataByHand(row.id).then(response => {
                this.rowMainId = row.id
                  this.isAdd = false
                  this.uploadData = response.data
                   this.handUploadLoading = false
                  this.$nextTick(() => {
                      this.$refs.handUpload.initData()
                  })
            }).catch(() => {
              this.handUploadLoading = false
            })
      } else {
          const loading = this.$loading()
            getUploadData(row.id).then(response => {
              this.tabResponseData = response.data
              this.tabOpen = true
              loading.close()
              this.$nextTick(() => {
                this.$refs.pcwDataMainTab.init()
                this.$refs.pcwDataMainTab.view = false
              })
            }).catch(() => {
              loading.close()
            })
        }
    },
    // 预览附件
    onlinePreview(rowId) {
      this.$refs.OnlinePreview.onlinePreviewFile(rowId)
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delPcwDataMain(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle(row) {
      this.downLoadExcel('/pcw/pcwDataMain/export/' + row.id)
    },
    downloadFile(row) {
      if (row.fileId) {
        for (let i = 0; i < row.fileId.length; i++) {
          const iframe = document.createElement('iframe')
          iframe.style.display = 'none'
          iframe.style.height = 0
          iframe.src = config.baseURL + '/file/fileBase/common/download?id=' + row.fileId[i] + '&Authorization=' + getToken()
          // 与前两种方式不同，iframe需要挂载到页面上才能触发请求
          document.body.appendChild(iframe)
          setTimeout(() => {
            iframe.remove()
          }, 1000)
        }
      } else {
        this.msgError(this.$t('没有附件'))
      }
    },
    // 财报点评form
    reviewForm(row) {
      this.reviewFormOpen = true
      this.$nextTick(() => {
        // 初始化
        this.$refs.pcwReviewForm.form = {}
        this.$refs.pcwReviewForm.form.mainId = row.id
      })
    },
    // 财报点评list
    reviewList(row) {
      this.reviewListOpen = true
      this.$nextTick(() => {
        // 初始化
        this.$refs.pcwReviewList.getList(row.id)
      })
    },
    convetTypeName(type) {
        if (type === 'hand') {
          return '手动录入'
        } else if (type === 'ocr') {
          return 'OCR识别'
        } else if (type === 'excel') {
           return 'Excel模板'
        } else {
          return type
        }
    },
    tipSave(data) {
          this.handUploadLoading = true
          var submit = {}
          submit.sheetData = data
          submit.mainId = this.rowMainId
          updataUploadByHand(submit).then(response => {
              this.handUploadLoading = false
              console.log('保存----')
              if (response.code === 200) {
                this.handOpen = false
                this.msgSuccess(this.$t('保存成功'))
                this.$nextTick(() => {
                  this.$emit('refresh')
                })
              } else {
                this.msgError(response.msg)
              }
          }).catch(() => {
            this.handUploadLoading = false
          })
    },
    updateForm(row) {
      this.updateformOpen = true
       this.$nextTick(() => {
        // 初始化
        this.$refs.pcwDataMainUploadForm.init(row)
      })
    },
    getValidationPass(data, index) {
      if (data) {
           return data.split(',')[index]
      }
      return 0
    }
  }
}
</script>

<style lang="scss" scoped>
  .reviewDialogForm{
    width: 60%;
    height: 50%;
    overflow: hidden;
    top: 5% !important;
    margin-top: unset!important;
  }
  .reviewDialogForm .el-dialog__body {
    height: calc(100% - 50px);
  }
  .reviewDialogForm .el-dialog__body>div{
    position: relative;
    height: 100%;
    width: 100%;

  }
  .reviewDialogForm .el-dialog__body>div .el-form{
    height: calc(100% - 50px);
    overflow: auto;
    position: absolute;
    top: 0;
    bottom: 40px;
    right: -20px;
    padding-right: 20px;
    left: 0;
  }
  .reviewDialogForm .el-dialog__body>div .dialog-footer{
    position: absolute;
    bottom: 0;
    right: 0;

  }
// .badge {
//   position: absolute;
//   right: 0;
//   top: 0;
//   background-color: #FF4E50;
//   color: #FFFFFF;
//   font-size: 14px;
//   line-height: 24px;
//   height: 24px;
//   padding: 0 14px;
//   text-align: center;
//   border-top-left-radius: 13px;
//   border-bottom-left-radius: 13px;
//   box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
//   transition: all 0.3s ease-out;
// }

// .badge:hover {
//   box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
//   transform: translateY(-2px);
// }
.badge {
  position: absolute;
  right: 0;
  top: 0;
  background-color: #f05654;
  color: #fff;
  font-size: 14px;
  line-height: 24px;
  height: 24px;
  padding: 0 12px;
  text-align: center;
  border-top-left-radius: 12px;
  border-bottom-left-radius: 12px;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.3);
  transition: all 0.3s ease-out;
}
 .badge:hover {
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  transform: translateY(-2px);
}
 .pre {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align:center;
    line-height: 18px;
    cursor:pointer;
}
.pre:hover {
  color:#409EFF;
  text-decoration: underline;
}

.el-dropdown {
  font-size: 12px;
}
// .el-dialog__wrapper {
//   min-width: 800px !important;
// }
// /deep/ .el-dialog__header {
//   display: none !important;
//   padding: 0px !important;
//   padding-bottom: 0px !important;
// }
/deep/.previewDialog {
    top: 0px !important;
    width: 100%;
    margin-top: unset !important;
    padding: 30px;
    padding-top: 15px;
}
</style>
